<?php
$imagesModel = new ImagesModel();
if (isset($this->comments)) {
    $count_comments = count($this->comments);
    if (!defined('ajax'))
        echo '<div id="comments">';
    echo '<h1>' . $this->t->_('Comments') . '</h1>';

    if ($count_comments == 0)
        echo '<div class="comments_no"><p>' . $this->t->_('No comments') . '</p></div>';

    $prelevel = 0;
    $comments = '';
    
    $i = 0;

    foreach ($this->comments as $key => $val) {
        $i++;
        $pos = $val->level + 1; //текущий уровень страницы
        if ($prelevel > $pos)
            $comments .= str_repeat('</li></ul>', $prelevel - $pos);
        if ($prelevel >= $pos)
            $comments .= '</li>';
        if ($prelevel < $pos) {
            if ($prelevel == 0) {//Начало всего списка
                $comments .= '<ul id="comments_tree">';
            } else {
                $comments .= '<ul>';
            }
        }
        $prelevel = $pos;
        if ($i == $count_comments) {
            $comments .= '<li><div class="comment lastcomment clearfix" id="comment_id_' . $val->id . '">';
        } else {
            $comments .= '<li><div class="comment clearfix" id="comment_id_' . $val->id . '">';
        }

        if ($val->uid > 0)
                $name = $val->name;
        else
            $name = $val->guest;
        
        $comments .= '<div class="comment_info">
                        <div class="comment_avatar">
                            <a href="#"><img src="' . $imagesModel->getAvatar($val->uid) . '" alt="' . $name . '"></a>
                        </div>
                        
                    </div>';

        $comments .= '<div class="comment_block">
                        <div class="comment_author"><a href="#">' . $name . '</a></div>
                        <div class="comment_content">' . $val->comment . '</div>
                        <div class="comment_datetime">' . date($this->config->time, strtotime($val->timestamp)) . ' | <a class="js-serv comment_post" href="#comment_id_' . $val->id . '" id="replay_comment_' . $val->id . '">' . $this->t->_('Reply') . '</a></div>
                      </div>';


        //$comments .= '<div class="comment_author"><img src="' . $imagesModel->getAvatar($val->uid) . '" alt="' . $val->name . '">' . $val->name . ' (' . date($this->config->time, strtotime($val->timestamp)) . ')</div>';
        //$comments .= '<div class="comment_content_only">' . $val->comment . '</div>';
        //$comments .= '<p class="comment_post"><a class="js-serv" href="#comment_id_' . $val->id . '" id="replay_comment_' . $val->id . '">' . $this->t->_('Reply') . '</a></p>';
        $comments .= '</div><div id="form_replay_comment_' . $val->id . '" class="reply_form">';
        if (isset($this->form) && $this->form->getValue('reply') == $val->id)
            $comments .= '<div class="form_comment clearfix"><h2>' . $this->t->_('Add new comment') . '</h2>' . $this->form . '</div>';
        $comments .= '</div>';




    }
    $comments .= '</li>' . str_repeat('</ul>', $prelevel);
    echo $comments;


    if (isset($this->form)) {
        echo '<div id="link_comment_0"><a class="js-serv comment_post" href="#replay_comment_0" id="replay_comment_0">' . $this->t->_('Add new comment') . '</a></div>';
        echo '<div id="form_replay_comment_0" class="reply_form">';

        if (isset($this->form) && $this->form->getValue('reply') == 0)
            echo '<div class="form_comment clearfix"><h2>' . $this->t->_('New comment') . '</h2>' . $this->form . '</div>';
        echo '</div>';
        echo "<script type=\"text/javascript\">
                $(document).ready(function() {
                    var options = { target: '#comments' };
                    $('#commentForm').ajaxForm(options);
                    $('#link_comment_0').hide();
                    $('a.comment_post').click(function() {
                        form = $('.form_comment').clone();
                        $('.form_comment').remove();
                        $('#form_' + this.id).append(form);
                        var str = this.id;

                        var id_reply = new Number(str.substring(str.lastIndexOf(\"_\") + 1, str.length));
                        if (id_reply > 0) {
                            $('#link_comment_0').show();
                        } else {
                            $('#link_comment_0').hide();
                        }

                        $('.form_comment #reply').val(this.id.replace('replay_comment_', ''));
                        $('#commentForm').ajaxForm(options);
                    });
                });
            </script>";
    } else {
        echo '<h2>' . $this->t->_('Add new comment') . '</h2>';
        echo '<div><p>' . $this->t->_('You are not logged in') . '</p></div>';
    }
    if (!defined('ajax'))
        echo '</div>';
}
